package com.tao.service.impl;

import com.tao.bean.Employee;
import com.tao.bean.EmployeeExample;
import com.tao.dao.EmployeeMapper;
import com.tao.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public List<Employee> getAll() {
        EmployeeExample example=new EmployeeExample();
        example.setOrderByClause("emp_id ASC");
        return employeeMapper.selectByExampleWithDept(example);
    }


    @Override
    public void saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
    }


    /*检查用户名是否可用
    * 查询不到结果count=0,返为ture,可用
    * */
    @Override
    public boolean checkHasEmp(String empName) {
        EmployeeExample example=new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);
        return count ==0;
    }

    @Override
    public Employee getEmp(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKeyWithDept(id);
        return employee;
    }

    @Override
    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public void deleteEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteEmpsByList(List<Integer> ids) {
        EmployeeExample example=new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
    }


}
